#include "spl.includes.h"
#include "splib.h"

using namespace SPLib;
using namespace SPLib::Windows;

#pragma region Protected Methods
void Dialog::wndProc(Event::WndProcEvent& iEvent)
{
  switch (iEvent.msg)
  {
  case WM_INITDIALOG:
    initWindow();
    iEvent.lResult = TRUE;
    onInitDialog(Event::InitDialogEvent(iEvent.wParam, iEvent.lParam));
    break;
  default:
    FrameWnd::wndProc(iEvent);
    break;
  }
}

INT_PTR Dialog::defDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  Event::WndProcEvent wEvent = { };

  Dialog *dlg;

  if (msg == WM_INITDIALOG)
  {
    dlg = reinterpret_cast<Dialog*>(lParam);
    dlg->mHandle = hWnd;
    dlg->setUserData(dlg);
  }
  else
  {
    dlg = reinterpret_cast<Dialog*>(fromHandle(hWnd));
  }

  if (dlg ? hWnd == dlg->mHandle : FALSE)
  {
    if (dlg->m_fRecursing)
    {
      return dlg->m_fRecursing = FALSE;
    }
    else
    {
      dlg->_lParam  = lParam;
      dlg->_wParam  = wParam;

      wEvent.wParam = wParam;
      wEvent.lParam = lParam;
      wEvent.msg    = msg;

      dlg->wndProc(wEvent);

      return wEvent.handled ?
        SetDlgMsgResult(dlg->mHandle, msg, wEvent.lResult) : FALSE;
    }
  }
  else
  {
    return FALSE;
  }
}

VOID Dialog::onInitDialog(Event::InitDialogEvent& iEvent)
{
}
#pragma endregion

#pragma region Management
LRESULT Dialog::destroy()
{
  return fModal ? !EndDialog(mHandle, TRUE) : WndBase::destroy();
}
#pragma endregion